//
//  MoreNotifyViewController.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/24.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import MJRefresh
import ObjectMapper

class MoreNotifyViewController: BaseTableViewController {
    
    var notifyArray = [NotifyModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "更多公告"
        view.backgroundColor = .white
        // Do any additional setup after loading the view.
        
        tableView.register(UINib(nibName: "NotifyTableViewCell", bundle: nil), forCellReuseIdentifier: "NotifyTableViewCell")
        tableView.estimatedRowHeight = 0
        tableView.rowHeight = 50
        
        header.isHidden = false
        
        tableView.reloadData()
        header.beginRefreshing()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func pullRefresh() {
        getNotifyData()
    }
    
    
    func getNotifyData() {
        let values = ["APPLB": "1"].convertToJsonString()
        
        let paramString = "<json>"+values+"</json>"
        
        NetworkingHandle.fetchNetworkData(method: "getNoticeList", paramString: paramString, success: {[weak self] (dict) in
            self?.header.endRefreshing()
            if let code = dict["ResultCode"] as? Int,code == 0 {
                if let imgsString = dict["NoticeLists"] as? String {
                    if let array = Mapper<NotifyModel>().mapArray(JSONString: imgsString.lowercased()) {
                        self?.notifyArray = array
                        self?.tableView.reloadData()
                    }
                }
                return
            }
//            if let code = dict["ResultCode"] as? Int,code == 1 {
//                if let msgString = dict["ResultMessage"] as? String {
//                    CDUtility.showMSG(message: msgString)
//                }
//            }
        }) {
            self.header.endRefreshing()
            print("")
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notifyArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NotifyTableViewCell", for: indexPath) as! NotifyTableViewCell
        cell.model = notifyArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // todo test
        let model = notifyArray[indexPath.row]
        let vc = NotifyDetailViewController()
        vc.notifyID = model.notifyNO
        navigationController?.pushViewController(vc, animated: true)
    }
}
